import { wxRequest } from '../../../utils/server.js'; 


const _wxRequest = new wxRequest();

const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl, 
    id: '', 
  },
  
  onLoad: function (options){  
    if (options.id){
      this.setData({
        id: options.id
      })
      this.getDetail()
    }
  }, 

  
  onShow:function(){ 
  },
 

  formSubmit: function (e) {
    var formdata = e.detail.value;  
    formdata.display = formdata.display?1:0;
    formdata.hot = formdata.hot ? 1 : 0;

    if (!formdata.catName) {
      wx.showToast({
        icon: 'none',
        title: '名称不能为空',
      })
      return 
    }

  
     
    console.log('formdata:', formdata) 
    this.formRequest(formdata)
  },

  //请求
  formRequest: function (formdata){
    var url = '/gds/protected/comm/beanCrud/gds.GoodsCatVO/create';
    var title = "发布成功"

    //修改
    if (this.data.cententDetail && this.data.cententDetail.id) {
      url = "/gds/protected/comm/beanCrud/gds.GoodsCatVO/update";
      formdata.id = this.data.cententDetail.id;
      title = "修改成功"
    }
    
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: formdata
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/publish/goodscat/list',
        })
      }, 1500)
    })
  },


  goback: function () {
    wx.navigateBack({})
  },
 
 //获取内容
 getDetail:function(){ 
   _wxRequest.wxTonkenAjax({
     url: app.globalData.apiUrl + "/gds/protected/comm/beanCrud/gds.GoodsCatVO/getById/"+this.data.id,
     method: 'get', 
   }, (res) => {
     this.setData({
       cententDetail:res.vo
     })
       
   })
 }



})